pacman::p_load(tidyverse, data.table, broom, plyr)
rm(list=ls())
`%!in%` = Negate(`%in%`) 
################################################################################


################################## Fertilizer imports

df = fread("../../data/factors/raw/FAOSTAT_fertilizer_prices.csv")
df = df[,c('Area','Element','Item','Year','Unit','Value')]
df_q = df[Element=='Import quantity' & Value>0,]
df_v = df[Element=='Import value',]

#Price
df = merge(df_q,df_v, by= c('Area','Item','Year'), suffixes = c('.q','.v'))
df$USD_per_t = (1000*df$Value.v/df$Value.q)
df_p = df[,c('Area','Item','Year','USD_per_t')]

#Expenditure weights
df_w1 = df[,c('Area','Year','Item','Value.v')]
df_w2 = df[,c('Area','Year','Value.v')][, lapply(.SD, sum, na.rm=TRUE), by=c('Area','Year')]
df = merge(df_w1,df_w2, by = c('Area','Year'))
df$share = df$Value.v.x/df$Value.v.y
df_w = df[,c('Area','Item','Year','share')]

#Expenditure-weighted prices
df = merge(df_p,df_w,by=c('Area','Item','Year'))
df$price_USD_per_t = df$USD_per_t*df$share
df_index = df[,c('Area','Year','price_USD_per_t')][, lapply(.SD, sum, na.rm=TRUE), by=c('Area','Year')]
setnames(df_index, new=c('country','year','price_USD_per_t'))
df_index$country = toupper(df_index$country)
write.csv(df_index, gzfile(paste0("../../data/factors/clean/fertilizer_prices.csv")), row.names = FALSE)




